// pages/geren/geren.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    picPath: app.globalData.picPath,
    userPhoto: '',
    imgFile: '',
    member: '',
    canvasWidth: 0,
    canvasHeight: 0,
    imgFlag: true,
    isUpload: false
  },




  // 退出登录
  exitHandle() {
    wx.showModal({
      title: '提示',
      content: '是否退出当前账号',
      complete: (res) => {
        if (res.confirm) {
          wx.clearStorageSync()
          wx.redirectTo({
            url: '/pages/login/login',
          })
        }
      }
    })
  },

  toPage(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;
    if (flag == 'password') {
      wx.navigateTo({
        url: '/gerenInfo/pages/passWord/passWord',
      })
    } else if (flag == 'signature') {
      wx.navigateTo({
        url: '/gerenInfo/pages/signature/signature',
      })
    } else if (flag == 'phoneNum') {
      wx.navigateTo({
        url: '/gerenInfo/pages/phoneNum/phoneNum',
      })
    }
  },


  errHandle(e) {
    this.setData({
      imgFlag: false
    })
  },

  // 上传头像
  uploadImgHandle() {
    var that = this;
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success(res) {
        console.log(res.tempFiles)
        that.setData({
          userPhoto: res.tempFiles[0].tempFilePath,
          imgFile: res.tempFiles[0],
        })
        that.getImg(res.tempFiles[0].tempFilePath)
        console.log(that.data.userPhoto);
        console.log(that.data.imgFile);
      }
    })
  },

  //获取图片信息
  getImg(url) {
    var that = this
    wx.getImageInfo({
      src: url,
      success: function (rr) {
        console.log(rr);
        var ctx = wx.createCanvasContext('attendCanvasId', that);
        var ratio = 1;
        var canvasWidth = rr.width
        var canvasHeight = rr.height;
        var quality = 0.3; //图片质量
        while (canvasWidth > 1000 || canvasHeight > 1000) {
          //比例取整
          canvasWidth = Math.trunc(rr.width / ratio)
          canvasHeight = Math.trunc(rr.height / ratio)
          ratio += 0.1;
        }
        quality = (quality + (ratio / 10)).toFixed(1);
        if (quality > 1) {
          quality = 1;
        }
        //设置canvas尺寸
        that.setData({
          canvasWidth: canvasWidth,
          canvasHeight: canvasHeight
        });
        ctx.drawImage(url, 0, 0, canvasWidth, canvasHeight); //将图片填充在canvas上
        ctx.draw();
        //下载canvas图片
        setTimeout(function () {
          wx.canvasToTempFilePath({
            canvasId: 'attendCanvasId',
            width: canvasWidth,
            height: canvasHeight,
            destWidth: canvasWidth,
            destHeight: canvasHeight,
            fileType: 'png',
            quality: quality,
            success: function success(path) {
              console.log(path);
              that.handleImg(path.tempFilePath)
              wx.hideLoading()
            },
            fail: function fail(e) {}
          }, that);
        }, 100);
      }
    })
  },

  // 上传图片
  uploadHandle(path) {
    console.log(path);
    wx.showLoading({
      title: '上传中',
    })
    var url = app.globalData.url + "member/updateImg",
      that = this;
    wx.uploadFile({
      url: url,
      filePath: path,
      formData: {
        member_id: app.globalData.memberId
      },
      name: 'file',
      success(res) {
        var res = JSON.parse(res.data)
        console.log(res);
        if (res.data == 'success') {
          wx.showToast({
            title: '更换成功',
            icon: 'none'
          })
          that.setData({
            isUpload: true
          })
          that.onLoad()
          app.getUserInfo()
          wx.hideLoading()
        } else {
          wx.showToast({
            title: '更换失败,请重试',
            icon: 'none'
          })
        }
      }
    })
  },


  handleImg(path) {
    this.setData({
      imgFile: path
    })
    this.uploadHandle(path)
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      member: app.globalData.member
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})